package com.example.flower_shop.service.impl;

import com.example.flower_shop.config.PasswordEncoderConfig;
import com.example.flower_shop.entity.User;
import com.example.flower_shop.repository.UserRepository;
import com.example.flower_shop.service.BaseService;
import com.example.flower_shop.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService, BaseService<User, Long> {

    private final UserRepository userRepository;
    private final PasswordEncoderConfig passwordEncoderConfig;

    @Override
    public Optional<User> findByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    @Override
    @Transactional
    public User register(String username, String password, String phone) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(passwordEncoderConfig.passwordEncoder().encode(password));
        user.setPhone(phone);
        user.setNickname(username); // 默认使用用户名作为昵称
        return userRepository.save(user);
    }

    @Override
    public Optional<User> findByOpenId(String openId) {
        return userRepository.findByOpenId(openId);
    }

    @Override
    @Transactional
    public User createOrUpdateByOpenId(String openId, String nickname, String avatarUrl, Integer gender) {
        User user = userRepository.findByOpenId(openId)
                .orElse(new User());

        user.setOpenId(openId);
        user.setNickname(nickname);
        user.setAvatarUrl(avatarUrl);
        user.setGender(gender);

        return userRepository.save(user);
    }

    @Override
    public boolean existsByOpenId(String openId) {
        return userRepository.existsByOpenId(openId);
    }

    @Override
    public Optional<User> findByPhone(String phone) {
        return userRepository.findByPhone(phone);
    }

    @Override
    @Transactional
    public User updatePhone(Long userId, String phone) {
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new RuntimeException("用户不存在"));

        user.setPhone(phone);
        return userRepository.save(user);
    }

    @Override
    public boolean existsByUsername(String username) {
        return userRepository.existsByUsername(username);
    }

    @Override
    public boolean existsByPhone(String phone) {
        return userRepository.existsByPhone(phone);
    }

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public Page<User> findAll(Pageable pageable) {
        return userRepository.findAll(pageable);
    }

    @Override
    public Optional<User> findById(Long id) {
        return userRepository.findById(id);
    }

    @Override
    @Transactional
    public User save(User entity) {
        return userRepository.save(entity);
    }

    @Override
    @Transactional
    public List<User> saveAll(Iterable<User> entities) {
        return userRepository.saveAll(entities);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        userRepository.deleteById(id);
    }

    @Override
    public boolean existsById(Long id) {
        return userRepository.existsById(id);
    }

    @Override
    public long count() {
        return userRepository.count();
    }
}